package com.iflytek.autofly.setting.biz.networking;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.base.EditActivity;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/12/2</td>
 * <td>14:50</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class NetPasswdEditActivity extends EditActivity {
    public static final String NEED_CONNECT = "need_connect";
    public static final String WLAN_NAME = "wlanName";
    public static final String SIGNAL_LEVEL = "signalLevel";
    public static final String AP_ID = "ap_id";
    public static final String NET_TYPE = "net_type";

    private boolean needConnect = false;
    private int apId = 0;
    private int mNetType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        addExpandView(R.layout.net_wifi_wlan_info);
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();
        final Bundle bundle = intent.getExtras();
        needConnect = bundle.getBoolean(NEED_CONNECT, false);
        apId = bundle.getInt(AP_ID, -1);
        {
            TextView tv = (TextView) findViewById(R.id.net_wifi_wlan_info_name_tv);
            tv.setText(bundle.getString(WLAN_NAME, ""));
        }
        {
            TextView tv = (TextView) findViewById(R.id.net_wifi_wlan_info_signal_level_tv);
            tv.setText(bundle.getString(SIGNAL_LEVEL, ""));
        }

        mNetType = bundle.getInt(NET_TYPE, -1);
        mSaveBtn.setEnabled(false);
        mEditEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int valueLength = 0;
                String chinese = "[\u4e00-\u9fa5]";
                for (int i = 0; i < s.length(); i++) {
                    String temp = s.subSequence(i, i + 1).toString();
                    if (temp.matches(chinese)) {
                        valueLength += 2;
                    } else {
                        valueLength++;
                    }
                }
                switch (mNetType) {
                    case AccessPoint.SECURITY_EAP:
                    case AccessPoint.SECURITY_PSK:
                        if (valueLength >= 8) {
                            mSaveBtn.setEnabled(true);
                        } else {
                            mSaveBtn.setEnabled(false);
                        }
                        break;
                    default:
                        if (valueLength != 0) {
                            mSaveBtn.setEnabled(true);
                        } else {
                            mSaveBtn.setEnabled(false);
                        }
                        break;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mShowPassword.setVisibility(View.VISIBLE);

        IntentFilter mFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(mReceiver, mFilter);
    }

    @Override
    protected void expandBundle(Bundle bundle) {
        super.expandBundle(bundle);
        bundle.putBoolean(NEED_CONNECT, needConnect);
        bundle.putInt(AP_ID, apId);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int mState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);
            if (mState == WifiManager.WIFI_STATE_DISABLING) {
                setResults();
                finish();
            }
        }
    };

    private void setResults() {
        setResult(RESULT_OK, new Intent());
    }
}
